//
// Created by zing on 3/28/2020.
//

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

#define BUFFER_SIZE 1024

int main() {
    int fd;
    char buf[BUFFER_SIZE];
    fd = open("test.txt", O_RDWR);
    if (fd < 0) {
        perror("open file error");
        return EXIT_FAILURE;
    }
    ssize_t i = read(fd, buf, BUFFER_SIZE);
    if (i < 0) {
        perror(strerror(errno));
    }
    printf("读取了%zd个字符\n",i);
    close(fd);
    char data[i];
    for (int j = 0; j < i; ++j) {
        data[j] = buf[j];
    }
    data[i] = '\0';
    printf("%s", data);
}